#include <stdio.h>
#include <string.h> 
#include "./include/protocol.h"
#include "./include/tcp_sxx.h"
#include "./include/io_test.h"
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "./include/adda_test.h"
// 发送协议命令
void send_command(int fd, const char *cmd) {
    
    write(fd, cmd, strlen(cmd));
    printf("发送命令：%s\n", cmd);
}
// 发送协议命令
void send_data(int fd, const char *data) {
    write(fd, data, strlen(data));
    printf("发送数据：%s\n", data);
}
// 发送响应
void send_response(int fd, const char *cmd, const char *result)
 {
    char response[100];
    snprintf(response, sizeof(response), "<%s%s>", cmd, result);
    write(fd, response, strlen(response));
    printf("发送响应：%s\n", response);
}
// 接收协议响应
int receive_response(int fd)
 {
    char buffer[256];
    int len = read(fd, buffer, sizeof(buffer) - 1);
    if (len > 0) {
        buffer[len] = '\0';
        printf("收到响应：%s\n", buffer);  
        return 1;

    } else {
        printf("接收失败或超时\n");
        return 0;
    }
    return len;
}

void packageinit_head( __u_char *sendbuf )
{
    sendbuf[0] = 0x55;
    sendbuf[1] = 0xAA;
    sendbuf[2] = 0x00;
    sendbuf[3] = 0x00;
}
void package_ending( __u_char* target_buf, __u_char* sources_buf, __u_short* len )
{
    sources_buf[(*len)++] = 0xAA;
    sources_buf[(*len)++] = 0x55;
    sources_buf[4] = *len;
    sources_buf[5] = *len >> 8;

    memcpy(target_buf, sources_buf, *len);

}

void recv_tcp_data_discard( int cmd_fd, int timeout )
{
    unsigned char buffer[BUFFER_SIZE];
    int valread;
    for( int i = 0; i < timeout; i++ )
    {
        usleep(500000);
        valread = recv(cmd_fd, buffer, BUFFER_SIZE, 0);   
    }
}
int handle_tcp_recv_data( unsigned char* data,int value )
{
    int ret;
    unsigned short len;
    unsigned short return_data_len;
    len = data[5] << 8 | data[4];
    return_data_len = len - 10;
    switch(data[6])
	{
		case Type_Start_Bit:
	
		case Type_Sycle_Bit:
			
		case Type_DDR3:
			
		case Type_Flash:
	
		case Type_Nvsram:

		case Type_Disk:
			if( data[7] == 0x00 )
            {
                switch(data[6])
                {
                    case Type_Start_Bit:
                        printf("✅ [上电Bit]测试成功!\n" );
                        break;
                    case Type_Sycle_Bit:
                        printf("✅ [周期Bit]测试成功!\n" );
                        break;
                    case Type_DDR3:
                        printf("✅ [DDR3测试]测试成功!\n" );
                        break;
                    case Type_Flash:
                        printf("✅ [Flash测试]测试成功!\n" );
                        break;
                    case Type_Nvsram:
                        printf("✅ [Nvsram测试]测试成功!\n" );
                        break;
                    case Type_Disk:
                        printf("✅ [电子盘测试]测试成功!\n" );
                        break;
                    default :
                        printf("功能类型不正确!\n");
                        break;
                }
            }
			else
            {
                switch(data[6])
                {
                    case Type_Start_Bit:
                        printf("✅ [上电Bit]测试失败!返回结果:\n" );
                        break;
                    case Type_Sycle_Bit:
                        printf("❌ [周期Bit]测试失败!返回结果:\n" );
                        break;
                    case Type_DDR3:
                        printf("❌ [DDR3测试]测试失败!返回结果:\n" );
                        break;
                    case Type_Flash:
                        printf("❌ [Flash测试]测试失败!返回结果:\n" );
                        break;
                    case Type_Nvsram:
                        printf("❌ [Nvsram测试]测试失败!返回结果:\n" );
                        break;
                    case Type_Disk:
                        printf("❌ [电子盘测试]测试失败!返回结果:\n" );
                        break;
                    default :
                        printf("功能类型不正确!\n");
                        break;
                }
                for( int i=0; i < return_data_len; i++ )
                {
                    printf("%02X ",data[i + 8]);
                }
                printf("\n");
            }
            break;
		case Type_Gpio:
			check_input_gpio_state(data, value );
			
			break;
			
		case Type_Adda:
			ret = check_ad_value(data, value );
			break;
			
		default : break;
	}
    return ret;
}
int handle_tcp_recv_package( int valread, unsigned char* buffer,int value )
{
    int ret = 0;
    
    if( buffer[0] != 0x55 || buffer[1] != 0xAA || buffer[valread -2] != 0xAA || buffer[valread -1] != 0x55  )
    {
        printf("Receive data check fail:");
        printf("Rame header and frame tail check error!\n");
        ret = -2;
    }
    if((buffer[5] << 8 | buffer[4]) != valread )
    {
        printf("Receive data check fail:");
        printf("Data length validation error!\n");
        ret = -3;
    }
    if( ret == 0)
    {
        ret = handle_tcp_recv_data(buffer,value);
    }
    return ret;
}